package com.yanqu.road.dataHelper.action;

import com.yanqu.road.dataHelper.action.excel.CrossOreWar;
import com.yanqu.road.dataHelper.action.excel.OreWar;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.manager.ServerMgr;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.servercenter.CrossServerInfo;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;


public class OreWarBoomExcel extends actionBase {

    public void go(List<Long> serverList){
//        abc();
//        String activityIdIn = "10067781";
        String activityIdIn = "9076806,9076807,9076808,9076809,9076810,9076811,9076812,9076813,9076814,9076815,9076816,9076817,9076818,9076819,9076820,9076821,9076822";
        List<Long> crossIdList = new ServerMgr().getActivityCrossIdList(StringUtils.stringToIntegerList(activityIdIn,","));
        List<CrossServerInfo> crossServerInfoList = new ServerMgr().getCrossServerInfoList(crossIdList);
        Map<Integer,List<ActivityConditionInfo>> condMap = new ServerMgr().getRankActivityRewardList(StringUtils.stringToIntegerList(activityIdIn,","));

        ExecutorService executorService = Executors.newFixedThreadPool(2);
        List<Future> futureList = new ArrayList<>();
        for(CrossServerInfo crossServerInfo : crossServerInfoList){
            Future future = executorService.submit(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    CrossOreWar crossOreWar = new CrossOreWar(crossServerInfo);
                    crossOreWar.go(activityIdIn);
                    return true;
                }
            });
            futureList.add(future);
            isResult(futureList);
        }
        Map<Integer, Map<String, UnionActivityConditionData>> data = DataMgr.getUnionData();
        Map<Long,Map<Integer,List<Long>>> serverIdActivityIdUserIdMap = new ConcurrentHashMap<>();
        for(Integer activityId : data.keySet()){
            Map<String, UnionActivityConditionData> unionMap = data.get(activityId);
            for(UnionActivityConditionData unionData : unionMap.values()){
                for(Long userId : unionData.getMemberUserIdList()){
                    Long serverId = userId / 10000000;
                    if(!serverIdActivityIdUserIdMap.containsKey(serverId)){
                        serverIdActivityIdUserIdMap.put(serverId,new ConcurrentHashMap<>());
                    }
                    if(!serverIdActivityIdUserIdMap.get(serverId).containsKey(activityId)){
                        serverIdActivityIdUserIdMap.get(serverId).put(activityId,new ArrayList<>());
                    }
                    serverIdActivityIdUserIdMap.get(serverId).get(activityId).add(userId);
                }
            }
        }
        List<Long> serverIdList = new ArrayList<>(serverIdActivityIdUserIdMap.keySet());
        for(Long serverId : serverIdList){
            if(ServerMgr.getServerInfoList().containsKey(serverId)){
                ServerInfo serverInfo = ServerMgr.getServerInfoList().get(serverId);
                Future future = executorService.submit(new Callable<Boolean>() {
                    @Override
                    public Boolean call() throws Exception {
                        GameServer testGameServer = new GameServer(serverInfo);
                        OreWar oreWar = new OreWar(testGameServer);
                        oreWar.go(serverIdActivityIdUserIdMap.get(serverId));
                        return true;
                    }
                });
                futureList.add(future);
            }
            isResult(futureList);
        }
        Map<Integer,Map<Long, Map<String,String>>> userData = DataMgr.getUserConditionData();
        for(Integer activityId : data.keySet()){
            Map<String, UnionActivityConditionData> unionConditionMap = data.get(activityId);
            for(UnionActivityConditionData unionData : unionConditionMap.values()){
                BigInteger userValue = BigInteger.ZERO;
                for(Long userId : unionData.getMemberUserIdList()){
                    if(userData.containsKey(activityId) && userData.get(activityId).containsKey(userId)){
                        userValue = userValue.add(new BigInteger(userData.get(activityId).get(userId).get("value")));
                    }
                }
                unionData.setPreValue(unionData.getValue());
                if(userValue.compareTo(unionData.getValue()) > 0){
                    System.err.println(unionData.getUnionUid() + "商会值" + unionData.getValue() + "区服玩家值和" + userValue);
                    unionData.setPreValue(userValue);
                }
            }
        }
        List<Map<String,String>> dataList = new ArrayList<>();
        Map<Integer,Map<String,Integer>> valueRankMap = getValueRankMap(data);
        Map<Integer,Map<String,Integer>> preValueRankMap = getPreValueRankMap(data);
        for(int activityId : valueRankMap.keySet()){
            Map<String,Integer> unionValueRankMap = valueRankMap.get(activityId);
            for(String unionUid : unionValueRankMap.keySet()){
                int valueRank = unionValueRankMap.get(unionUid);
                int preValueRank = preValueRankMap.get(activityId).get(unionUid);
                if(valueRank > preValueRank){
                    for(Long userId : data.get(activityId).get(unionUid).getMemberUserIdList()){
                        int position = data.get(activityId).get(unionUid).getMemberPositionMap().get(userId);
                        boolean isMaster = position == eUnionPosition.Master.getValue();
                        Property valueReward = getReward(condMap.get(activityId),valueRank,isMaster);
                        Property preValueReward = getReward(condMap.get(activityId),preValueRank,isMaster);
                        Map<String,String> tempMap = new ConcurrentHashMap<>();
                        tempMap.put("activityId",""+activityId);
                        tempMap.put("userId",""+userId);
                        tempMap.put("unionUid",""+unionUid);
                        tempMap.put("是不是会长",""+isMaster);
                        tempMap.put("之前的排名",""+valueRank);
                        tempMap.put("重新后的排名",""+preValueRank);
                        tempMap.put("之前的奖励",""+PropertyHelper.parsePropertyToString(valueReward));
                        tempMap.put("重新后的奖励",""+PropertyHelper.parsePropertyToString(preValueReward));
                        tempMap.put("要补的奖励",""+PropertyHelper.parsePropertyToString(removeProperty(valueReward,preValueReward)));
                        dataList.add(tempMap);
                    }
                }
            }
        }
        DataMgr.addData(dataList);
        System.err.println("===========================================");
    }

    private Property removeProperty(Property valueReward,Property nowReward){
        Property nProperty = new Property(nowReward);
        for(int goodId : valueReward.getGoods().keySet()){
            nProperty.removePropertyBug(goodId,valueReward.getGoods().get(goodId));
        }
        return nProperty;
    }

    //用value作为排名
    private Map<Integer,Map<String,Integer>> getValueRankMap(Map<Integer, Map<String, UnionActivityConditionData>> unionData){
        Map<Integer,Map<String,Integer>> resMap = new ConcurrentHashMap<>();
        for(Integer activityId : unionData.keySet()){
            Map<String, UnionActivityConditionData> unionMap = unionData.get(activityId);
            List<UnionActivityConditionData> unionList = new ArrayList<>(unionMap.values());
            unionList.sort(new ValueSort());
            if(!resMap.containsKey(activityId))resMap.put(activityId,new ConcurrentHashMap<>());
            for(int i = 1;i <= unionList.size();i++){
                resMap.get(activityId).put(unionList.get(i-1).getUnionUid(),i);
            }
        }
        return resMap;
    }

    static class ValueSort implements Comparator<UnionActivityConditionData> {
        @Override
        public int compare(UnionActivityConditionData o1, UnionActivityConditionData o2) {
            if (o1.getValue().compareTo(o2.getValue()) == 0) {
                return 0;
            }
            if (o1.getValue().compareTo(o2.getValue()) >= 0) {
                return -1;
            }
            return 1;
        }
    }

    private Map<Integer,Map<String,Integer>> getPreValueRankMap(Map<Integer, Map<String, UnionActivityConditionData>> unionData){
        Map<Integer,Map<String,Integer>> resMap = new ConcurrentHashMap<>();
        for(Integer activityId : unionData.keySet()){
            Map<String, UnionActivityConditionData> unionMap = unionData.get(activityId);
            List<UnionActivityConditionData> unionList = new ArrayList<>(unionMap.values());
            unionList.sort(new PreValueSort());
            if(!resMap.containsKey(activityId))resMap.put(activityId,new ConcurrentHashMap<>());
            for(int i = 1;i <= unionList.size();i++){
                resMap.get(activityId).put(unionList.get(i-1).getUnionUid(),i);
            }
        }
        return resMap;
    }

    static class PreValueSort implements Comparator<UnionActivityConditionData> {
        @Override
        public int compare(UnionActivityConditionData o1, UnionActivityConditionData o2) {
            if (o1.getPreValue().compareTo(o2.getPreValue()) == 0) {
                return 0;
            }
            if (o1.getPreValue().compareTo(o2.getPreValue()) >= 0) {
                return -1;
            }
            return 1;
        }
    }

    private Property getReward(List<ActivityConditionInfo> conditionList,int rank,boolean isMaster){
        for(ActivityConditionInfo conditionInfo : conditionList){
            if(BigInteger.valueOf(rank).compareTo(conditionInfo.getParamList().get(0)) >= 0 && BigInteger.valueOf(rank).compareTo(conditionInfo.getParamList().get(1)) <= 0){
                if(isMaster){
                    return conditionInfo.getMasterReward();
                }else{
                    return conditionInfo.getReward();
                }
            }
        }
        return new Property();
    }

    private void abc(){
        String userStr = "201006050000255,201006050013571,201006050011249,201006050003974,201006050013438,201006050012718,201006050006355,201006050002047,201006050000061,201006050000045,201006050005962,201006050005828,201006050006729,201006050000255,201006050013571,201006050011249,201006050003974,201006050013438,201006050012718,201006050006355,201006050002047,201006050000061,201006050000045,201006050005828,201006050006729,201006050005962,201006050000255,201006050013571,201006050011249,201006050003974,201006050013438,201006050012718,201006050006355,201006050002047,201006050000061,201006050000045,201006050005828,201006050006729,201006050005962,201006050000255,201006050013571,201006050011249,201006050003974,201006050013438,201006050012718,201006050002047,201006050005828,201006050005962,201006050000045,201006050006355,201006050000061,201006050006729,201006050000255,201006050013571,201006050011249,201006050003974,201006050002047,201006050013438,201006050005828,201006050012718,201006050005962,201006050000045,201006050006355,201006050000061,201006050006729,201006050000255,201006050013571,201006050011249,201006050013438,201006050005962,201006050012718,201006050003974,201006050002047,201006050000045,201006050006355,201006050000061,201006050005828,201006050006729,201006050000255,201006050000045,201006050002047,201006050013438,201006050011249,201006050013571,201006050012718,201006050003974,201006050006355,201006050000061,201006050005962,201006050005828,201006050006729,201006050009613,201006050009613,201006050009613,201006050009613,201006050009613,201006050009613,201006050009613,201006050012245,201006050005195,201006050012245,201006050005195,201006050012245,201006050005195,201006050012245,201006050005195,201006050012245,201006050005195,201006050012245,201006050005195,201006050012245,201006050005195,201006050009227,201006050009227,201006050009227,201006050009227,201006050009227,201006050009227,201006050009227,201006050008064,201006050012486,201006050008064,201006050012486,201006050008064,201006050012486,201006050008064,201006050012486,201006050008064,201006050012486,201006050008064,201006050012486,201006050008064,201006050012486,201006050008823,201006050008823,201006050008823,201006050008823,201006050008823,201006050008823,201006050008823,201006050000678,201006050000306,201006050003148,201006050006556,201006050010843,201006050000004,201006050000678,201006050000306,201006050003148,201006050006556,201006050010843,201006050000004,201006050000678,201006050000306,201006050003148,201006050006556,201006050010843,201006050000004,201006050000678,201006050000306,201006050003148,201006050006556,201006050010843,201006050000004,201006050000678,201006050000306,201006050003148,201006050006556,201006050010843,201006050000004,201006050000678,201006050000306,201006050006556,201006050003148,201006050010843,201006050000004,201006050000678,201006050003148,201006050006556,201006050010843,201006050000306,201006050000004,201006050006107,201006050006107,201006050006107,201006050006107,201006050006107,201006050006107,201006050006107,201006050000340,201006050000340,201006050000340,201006050000340,201006050000340,201006050000340,201006050000340,201006050013748,201006050006061,201006050013748,201006050006061,201006050013748,201006050006061,201006050013748,201006050006061,201006050013748,201006050006061,201006050013748,201006050006061,201006050013748,201006050006061,201006050006188,201006050006188,201006050006188,201006050006188,201006050006188,201006050006188,201006050006188,201006050003555,201006050003555,201006050003555,201006050003555,201006050003555,201006050003555,201006050003555,201006050011264,201006050011264,201006050011264,201006050011264,201006050011264,201006050011264,201006050011264,201006050000972,201006050009289,201006050004625,201006050002059,201006050000794,201006050009975,201006050004250,201006050001465,201006050006321,201006050010358,201006050012490,201006050013588,201006050009003,201006050013701,201006050000576,201006050003059,201006050004466,201006050006650,201006050010335,201006050000972,201006050009289,201006050004625,201006050002059,201006050000794,201006050009975,201006050004250,201006050001465,201006050006321,201006050010358,201006050012490,201006050009003,201006050013588,201006050013701,201006050000576,201006050003059,201006050004466,201006050006650,201006050010335,201006050000972,201006050009289,201006050004625,201006050002059,201006050000794,201006050009975,201006050004250,201006050001465,201006050009003,201006050006321,201006050010358,201006050012490,201006050013588,201006050013701,201006050003059,201006050004466,201006050006650,201006050000576,201006050010335,201006050000972,201006050009289,201006050004625,201006050002059,201006050000794,201006050009975,201006050009003,201006050004250,201006050001465,201006050006321,201006050010358,201006050012490,201006050013588,201006050013701,201006050003059,201006050004466,201006050006650,201006050000576,201006050010335,201006050000972,201006050009289,201006050004625,201006050002059,201006050009003,201006050000794,201006050009975,201006050004250,201006050001465,201006050006321,201006050010358,201006050012490,201006050013588,201006050013701,201006050003059,201006050004466,201006050006650,201006050000576,201006050010335,201006050000972,201006050009289,201006050000794,201006050004625,201006050009003,201006050002059,201006050009975,201006050004250,201006050001465,201006050006321,201006050010358,201006050012490,201006050013588,201006050013701,201006050003059,201006050004466,201006050006650,201006050000576,201006050010335,201006050000972,201006050009975,201006050009003,201006050000794,201006050009289,201006050004625,201006050002059,201006050004250,201006050001465,201006050006321,201006050010358,201006050012490,201006050013588,201006050013701,201006050003059,201006050004466,201006050006650,201006050000576,201006050010335,201006050008747,201006050012602,201006050008747,201006050012602,201006050008747,201006050012602,201006050008747,201006050012602,201006050008747,201006050012602,201006050008747,201006050012602,201006050008747,201006050012602,201006050009138,201006050009138,201006050009138,201006050009138,201006050009138,201006050009138,201006050009138,201006050001735,201006050010803,201006050001735,201006050010803,201006050001735,201006050010803,201006050001735,201006050010803,201006050001735,201006050010803,201006050001735,201006050010803,201006050001735,201006050010803,201006050011485,201006050011485,201006050011485,201006050011485,201006050011485,201006050011485,201006050011485,201006050003561,201006050003561,201006050003561,201006050003561,201006050003561,201006050003561,201006050003561,201006050003059,201006050006677,201006050003059,201006050006677,201006050003059,201006050006677,201006050003059,201006050006677,201006050003059,201006050006677,201006050003059,201006050006677,201006050003059,201006050006677,201006050009069,201006050009069,201006050009069,201006050009069,201006050009069,201006050009069,201006050009069,201006050012281,201006050009697,201006050003803,201006050012281,201006050009697,201006050003803,201006050012281,201006050009697,201006050003803,201006050012281,201006050009697,201006050003803,201006050012281,201006050009697,201006050003803,201006050012281,201006050009697,201006050003803,201006050012281,201006050009697,201006050003803,201006050005635,201006050000140,201006050001719,201006050005635,201006050000140,201006050001719,201006050005635,201006050000140,201006050001719,201006050005635,201006050000140,201006050001719,201006050005635,201006050000140,201006050001719,201006050005635,201006050000140,201006050001719,201006050005635,201006050000140,201006050001719,201006050002033,201006050002033,201006050002033,201006050002033,201006050002033,201006050002033,201006050002033,201006050002160,201006050009553,201006050002160,201006050009553,201006050002160,201006050009553,201006050002160,201006050009553,201006050002160,201006050009553,201006050002160,201006050009553,201006050002160,201006050009553,201006050000004,201006050000004,201006050000004,201006050000004,201006050000004,201006050000004,201006050000004,201006050012511,201006050012511,201006050012511,201006050012511,201006050012511,201006050012511,201006050012511,201006050012280,201006050012280,201006050012280,201006050012280,201006050012280,201006050012280,201006050012280,201006050000330,201006050012407,201006050011918,201006050011406,201006050000330,201006050012407,201006050011918,201006050011406,201006050000330,201006050012407,201006050011918,201006050011406,201006050000330,201006050012407,201006050011918,201006050011406,201006050000330,201006050012407,201006050011918,201006050011406,201006050000330,201006050012407,201006050011406,201006050011918,201006050000330,201006050011406,201006050012407,201006050011918,201006050011447,201006050011447,201006050011447,201006050011447,201006050011447,201006050011447,201006050011447,201006050003988,201006050000329,201006050007173,201006050007793,201006050004577,201006050011960,201006050000228,201006050007531,201006050003988,201006050000329,201006050007173,201006050007793,201006050004577,201006050011960,201006050000228,201006050007531,201006050003988,201006050000329,201006050007173,201006050007793,201006050004577,201006050011960,201006050000228,201006050007531,201006050003988,201006050000329,201006050007173,201006050007793,201006050004577,201006050011960,201006050000228,201006050007531,201006050003988,201006050000329,201006050007173,201006050007793,201006050004577,201006050011960,201006050000228,201006050007531,201006050003988,201006050000329,201006050007173,201006050007531,201006050007793,201006050004577,201006050011960,201006050000228,201006050003988,201006050007173,201006050000329,201006050007531,201006050007793,201006050004577,201006050011960,201006050000228,201006050003301,201006050013346,201006050003301,201006050013346,201006050003301,201006050013346,201006050003301,201006050013346,201006050003301,201006050013346,201006050003301,201006050013346,201006050003301,201006050013346,201006050009440,201006050009440,201006050009440,201006050009440,201006050009440,201006050009440,201006050009440,201006050007945,201006050012758,201006050001155,201006050002416,201006050001468,201006050000831,201006050001559,201006050000764,201006050000071,201006050007945,201006050012758,201006050001155,201006050002416,201006050001468,201006050000831,201006050001559,201006050000764,201006050000071,201006050007945,201006050012758,201006050001155,201006050002416,201006050001468,201006050000831,201006050001559,201006050000764,201006050000071,201006050007945,201006050012758,201006050001155,201006050002416,201006050001468,201006050000831,201006050001559,201006050000764,201006050000071,201006050007945,201006050012758,201006050001155,201006050002416,201006050000071,201006050000831,201006050000764,201006050001468,201006050001559,201006050007945,201006050012758,201006050000764,201006050000831,201006050001155,201006050001468,201006050001559,201006050000071,201006050002416,201006050007945,201006050002416,201006050012758,201006050001155,201006050001559,201006050000071,201006050001468,201006050000831,201006050000764,201006050002310,201006050007693,201006050009568,201006050002310,201006050007693,201006050009568,201006050002310,201006050007693,201006050009568,201006050002310,201006050007693,201006050009568,201006050002310,201006050007693,201006050009568,201006050002310,201006050007693,201006050009568,201006050002310,201006050009568,201006050007693,201006050010645,201006050007917,201006050000630,201006050006529,201006050009303,201006050009629,201006050000445,201006050001121,201006050004998,201006050010645,201006050007917,201006050000630,201006050006529,201006050009303,201006050009629,201006050000445,201006050001121,201006050004998,201006050010645,201006050007917,201006050000630,201006050006529,201006050009303,201006050009629,201006050000445,201006050001121,201006050004998,201006050010645,201006050007917,201006050000630,201006050006529,201006050009303,201006050009629,201006050000445,201006050004998,201006050001121,201006050010645,201006050007917,201006050000630,201006050006529,201006050004998,201006050000445,201006050009303,201006050009629,201006050001121,201006050010645,201006050007917,201006050006529,201006050009303,201006050000445,201006050009629,201006050000630,201006050004998,201006050001121,201006050010645,201006050007917,201006050000445,201006050009629,201006050000630,201006050006529,201006050004998,201006050009303,201006050001121,201006050001356,201006050006650,201006050001485,201006050012493,201006050002287,201006050010335,201006050000896,201006050004985,201006050003106,201006050013452,201006050001356,201006050006650,201006050001485,201006050012493,201006050002287,201006050010335,201006050000896,201006050004985,201006050003106,201006050013452,201006050001356,201006050006650,201006050001485,201006050012493,201006050002287,201006050010335,201006050000896,201006050004985,201006050003106,201006050013452,201006050001356,201006050006650,201006050001485,201006050012493,201006050002287,201006050010335,201006050000896,201006050004985,201006050003106,201006050013452,201006050001356,201006050006650,201006050001485,201006050012493,201006050002287,201006050004985,201006050000896,201006050003106,201006050010335,201006050013452,201006050001356,201006050006650,201006050002287,201006050004985,201006050000896,201006050001485,201006050012493,201006050010335,201006050003106,201006050013452,201006050001356,201006050006650,201006050004985,201006050012493,201006050002287,201006050010335,201006050000896,201006050003106,201006050001485,201006050013452,201006050012914,201006050012914,201006050012914,201006050012914,201006050012914,201006050012914,201006050012914,201006050002440,201006050002440,201006050002440,201006050002440,201006050002440,201006050002440,201006050002440,201006050007875,201006050007875,201006050007875,201006050007875,201006050007875,201006050007875,201006050007875";
        List<Long> userIdList = StringUtils.stringToLongList(userStr,",");
        Map<Long,Long> userIdMap = new ConcurrentHashMap<>();
        for(Long userId : userIdList){
            if(!userIdMap.containsKey(userId)){
                userIdMap.put(userId,1L);
            }
        }
        System.out.println(1);
    }
}
